<template>
	<view class="bg-white padding-lr">
		<u-form :model="form" ref="uForm" label-width="auto">
			<u-form-item label="服务" prop="execId" required>
				<view>{{form.execName}}</view>
			</u-form-item>
			<u-form-item label="编码" prop="code" required>
				<u-input v-model="form.code" />
			</u-form-item>
			<u-form-item label="名称" prop="name" required>
				<u-input v-model="form.name" />
			</u-form-item>
			<u-form-item label="路径" prop="path" required>
				<u-input v-model="form.path" />
			</u-form-item>
			<u-form-item label="cron" prop="cron" required>
				<u-input v-model="form.cron" />
			</u-form-item>
			<u-form-item label="负责人" prop="person" required>
				<u-input v-model="form.person" />
			</u-form-item>
			<u-form-item label="邮箱" prop="email" required>
				<u-input v-model="form.email" />
			</u-form-item>
			<u-form-item label="重试" prop="retry" required>
				<u-input type="number" v-model="form.retry" />
			</u-form-item>
			<u-form-item label="参数" prop="params">
				<u-input v-model="form.params" />
			</u-form-item>
			<u-form-item label="说明" prop="jobDesc">
				<u-input v-model="form.jobDesc" />
			</u-form-item>
			<view class="padding-tb">
				<u-button type="primary" @click="save()">保存</u-button>
			</view>
		</u-form>
	</view>
</template>

<script>
	export default {
		onLoad: function(option) {
			if (option.id) {
				this.form.id = option.id;
				this.get();
			} else if (option.execId) {
				this.form.execId = option.execId;
				this.form.execName = option.execName;
			} else {
				this.$utils.modal("提示", "请选择执行器", false, flag => {
					if (flag) {
						uni.navigateBack();
					}
				});
			}
		},
		onShow: function() {},
		onReady: function() {
			this.$refs.uForm.setRules(this.rules);
		},
		onReachBottom: function() {},
		data() {
			return {
				form: {
					id: null,
					execId: null,
					execName: null,
					code: null,
					name: null,
					path: null,
					cron: "0 0/5 * * * ?",
					person: null,
					email: null,
					retry: 1,
					params: null,
					status: null,
					jobDesc: null,
				},
				rules: {
					execId: [{
						required: true,
						message: "请选择服务"
					}],
					code: [{
						required: true,
						message: "请填写编码"
					}],
					name: [{
						required: true,
						message: "请填写名称"
					}],
					path: [{
						required: true,
						message: "请填写地址"
					}],
					cron: [{
						required: true,
						message: "请填写CRON表达式"
					}],
					person: [{
						required: true,
						message: "请填写责任人"
					}],
					email: [{
						required: true,
						message: "请填写责任人email"
					}],
					retry: [{
						required: true,
						message: "请填写重试次数"
					}]
				}
			};
		},
		methods: {
			save() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.$http.post(`/admin/sys/scheduler/job/save`, this.form, res => {
							const eventChannel = this.getOpenerEventChannel();
							eventChannel.emit('refresh', {});
							this.$utils.modal("提示", "保存成功！", false, flag => {
								if (flag) {
									uni.navigateBack();
								}
							})
						}, false, true, true);
					}
				});
			},
			get() {
				this.$http.get(`/admin/sys/scheduler/job/get/${this.form.id}`, {}, res => {
					this.form = res.data || this.form;
				}, false, true, true);
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>
